<?php
/**
 * @license    GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.view');

/**
 * HTML View class for the CommunalMeals Component
 *
 */

class CommunalMealsViewEventGroups extends JView
{ 
	private $startDate;
	private $days;
	
	function getCMealsMenu() {
		global $option;

		$startDateTime = strtotime($this->startDate);
		$backwardStartDate = date("Y-m-d", $startDateTime - ($this->days*86400));
		$today = date("Y-m-d");
		$forwardStartDate = date("Y-m-d", $startDateTime + ($this->days*86400));
		
		$menu = new CMealsMenu();
		$menu->addLink("LIST_PREVIOUS", $this->baseurl."/index.php?option=".$option."&view=eventgroups&startdate=".$backwardStartDate);
		$menu->addSeparator();
		$menu->addLink("LIST_FROM_TODAY", $this->baseurl."/index.php?option=".$option."&view=eventgroups&startdate=".$today);
		$menu->addSeparator();
		$menu->addLink("LIST_NEXT", $this->baseurl."/index.php?option=".$option."&view=eventgroups&startdate=".$forwardStartDate);
		$menu->addBigSeparator();
		$menu->addLink("AUTO_SIGNUP", $this->baseurl."/index.php?option=".$this->option."&view=signupauto&event_type=".$this->eventType);
		return $menu;
	}

	function display($tpl = null)
	{
		global $option, $mainframe;
		
		// Get optional params from request
		$this->startDate = JRequest::getVar('startdate', NULL );
		$this->days = JRequest::getVar('days', NULL );

		// Get/set session vars
		$session =& JFactory::getSession();
		if (is_null($this->startDate)) {
			$this->startDate = $session->get( "startDate", date("Y-m-d"), "cmeals" );
		}
		else {
			$session->set( "startDate", $this->startDate, "cmeals" ); 
		}
		if (is_null($this->days)) {
			$this->days = $session->get( "days", 35, "cmeals" );
		}
		else {
			$session->set( "days", $this->days, "cmeals" );
		}
		
		// Get data from model
		$eventType = CMeals::TYPE_COMMUNAL_MEAL;
		// Find next 5 events of this type:
		$eventProspectList = CMeals::readEventListByDate($this->startDate, NULL, $eventType, 5);
        // Find the distinct eventGroups and enrich them with info
		$eventGroupList = array();
        foreach ($eventProspectList as $eventProspect) {
        	$eventGroupList[$eventProspect->group_id] = CMeals::readEventGroup($eventProspect->group_id);
        }
		$member = CMeals::readMember(CMealsFrontEndUtils::getCurrentMemberId());
        foreach ($eventGroupList as $eventGroup) {
        	$eventGroup->eventList = CMeals::readEventList($eventGroup->id);
        	CMealsSignup::appendSignupData($eventGroup->eventList, $member);
        	$eventGroup->crewList = CMeals::readCrewList($eventGroup->id);
        }
		$categoryList = CMeals::readEventCategoryList($eventType);
        
		// Pass data to template
        $this->assignRef( 'eventType', $eventType );
		$this->assignRef( 'eventGroupList', $eventGroupList );
		$this->assignRef( 'categoryList', $categoryList );
        $this->assignRef( 'member', $member );
        $this->assignRef( 'option', $option );
        $this->assignRef( 'cmealsMenu', $this->getCMealsMenu() );
        
        // Select appropriate subtemplate for event type
        $tpl = strtolower($eventType);
        
		parent::display($tpl);
	}
}